package com.test.zhangxin.jianpin;

import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

    private int space;
    private int spanIndex = 0;
    private int spanCount = 1;

    public SpaceItemDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
        spanIndex = lp.getSpanIndex();
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        spanCount = layoutManager.getSpanCount();
        int left = space * (spanCount - spanIndex) / spanCount;
        int right = (spanIndex + 1) * space / spanCount;
        outRect.set(left, space,right, 0);
    }
}